<link rel="stylesheet" href="${request.contextPath}/static/oms/default/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<div class="row">
	<div class="col-md-12">
		<form id="userAssignRoleForm" action="${request.contextPath}/system/authority/user/assign/role" method="post" onsubmit="return false">
			<input type="hidden" id="id" name="id" value="${id}">
			<div class="modal-body">
				<div class="form-group">
					<label>选择角色：</label>
					<input type="checkbox" id="checkAllNodes" class="flat-red" >全选
					<ul id="treeDemo" class="ztree" style="margin-top: 10px;
														    border: 1px solid #617775;
														    background: #f0f6e4;
														    width: 220px;
														    height: 360px;
														    overflow-y: scroll;
														    overflow-x: auto;"></ul>
				</div>
			</div>
			<div class="modal-footer">
				<div class="pull-right">
					<button type="button" class="btn btn-primary btn-sm" ><i class="fa fa-save"></i> 保存</button>
					<button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><i class="fa fa-close"></i> 取消</button>
				</div>
			</div>
		</form>
	</div>
</div>
<script type="text/javascript" src="${request.contextPath}/static/oms/default/plugins/zTree/jquery.ztree.all.js"></script>

<script type="text/javascript">
function ajaxSubmit(form) {
	
	var $form = $(form);
	
	if ($form.length === 1 && $form.validate()) {
		$.post(form.action, $form.serialize(),
			function(result) {
				if (result.success) {
					$("#lgModal").modal('hide');
		        	alertMsg(result.message,"success");
		        	reloadTable(user_table);
				} else {
					$form.find(".error-msg").show();
					$form.find(".error-msg").html(result.message);
				}
			}, 'json');
	}
}

var setting = {
	check: {
		enable: true,
		chkStyle: "checkbox",  
        chkboxType: { "Y": "p", "N": "s" }
	},
	data: {
		simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: null
        }
    },
	async: {
		enable: true,
		url:"/system/authority/role/getRolesTree.action"
	},
	callback: {
		onAsyncSuccess:function(event, treeId, treeNode, msg){
			$.get('/system/authority/role/getRoleByUserId.action', {
				userId : $("#userAssignRoleForm").find('input[name="id"]').val()
			}, function(result) {
				var treeObj=$.fn.zTree.getZTreeObj(treeId);
				if (result) {
					for (var i = 0; i < result.length; i++) {
						var node = treeObj.getNodeByParam("id", result[i].id);     
						if(node){
						   	node.checked=true;  
					    } 
						treeObj.updateNode(node,true);
					}
				}
				treeObj.expandAll(true);//全部展开 

			}, 'json');

		    
		}
	}
};


$(function() {

	$("#checkAllNodes").on('click', function (e) {
		var treeObj=$.fn.zTree.getZTreeObj("treeDemo");	
		if(this.checked){
			treeObj.checkAllNodes(true);
		}else{
			treeObj.checkAllNodes(false);
		}
		
	});
	
	var tree= $.fn.zTree.init($("#treeDemo"), setting);
	
	$userAssignRoleForm = $("#userAssignRoleForm");
	$userAssignRoleForm.on('click',".btn-primary", function (e) {
		var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
        nodes=treeObj.getChangeCheckedNodes(true);
		var idArr = new Array();
		for(i = 0; i < nodes.length; i++) {
		 idArr[i] = nodes[i].id;
		}

		$.post('/system/authority/user/assign/role.action', {
			userId : $userAssignRoleForm.find('input[name="id"]').val(),
			ids : idArr.join(',')
		}, function(result) {
			if (result.success) {
				$("#lgModal").modal('hide');
	        	alertMsg(result.message,"success");
	        	reloadTable(user_table);
			} else {
				alertMsg(result.message,"error");
			}
		}, 'json');
	});
	
	
});
</script>